package com.hmall.api.config;

import com.hmall.api.fallback.ItemClientFallbackFactory;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@Configuration
public class FeignClientConfig {

    /*
  开枪Feign的日志记录
   */
    @Bean
    feign.Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    /**
     * 添加feign的拦截器使得微服务内部调用添加上token
     * @return
     */
    @Bean
    public RequestInterceptor requestInterceptor(){

        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                if (UserContext.getUser()!=null) {

                    requestTemplate.header("user-info", UserContext.getUser().toString());
                }
            }

        };
    }

    @Bean
    public ItemClientFallbackFactory iteamClientFallbackFactory (){
        return  new ItemClientFallbackFactory();
    }
}
